package com.kins.executer.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.kins.executer.constants.Constant;
import com.kins.executer.constants.DataConstant;
import com.kins.executer.service.ExecuteService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: 申科
 * @Date:2025/11/12 17:08
 */

public class ExecuteServiceImpl implements ExecuteService {

    @Resource(name = "droolsBusinessService")
    private DataBusinessService droolsBusinessService;

    @Resource(name = "liteFlowBusinessService")
    private DataBusinessService liteFlowBusinessService;

    @Value("${execute.config.type}")
    private String executerType;

    private static final Logger logger = LoggerFactory.getLogger(ExecuteServiceImpl.class);
    @Override
    public String execute(Map<String, Object> inputMap, String method, String path) {
        String returnStr = "";
        //当前版本
        String version = (String) inputMap.get(DataConstant.INPUTDATA_VERSION);
        //当前入参对象
        Map<String, Object> businessInput = (Map<String, Object>) inputMap.get(DataConstant.INPUTDATA_BUSINESSINPUT);
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> resultMap = new HashMap<>();
        String status = ReturnCodeEnum.SUCCESS.getCode();
        String info = ReturnCodeEnum.SUCCESS.getDesc();
        try{
            if("drools".equals(executerType)){
                resultMap = droolsBusinessService.engineService(path,method,version,businessInput);
            }else {
                resultMap = liteFlowBusinessService.engineService(path,method,version,businessInput);
            }
            Map<String, Object> sysMap = (Map<String, Object>) resultMap.get("System");
            if(!("1".equals(sysMap.get("isSuccess")))){
                status = ReturnCodeEnum.NO_SUCCESS.getCode();
                info = "接口执行失败:"+sysMap.get("exception");
            }
        }catch (Exception e){
            status = ReturnCodeEnum.NO_SUCCESS.getCode();
            info = IOUtils.getStackTraceMsg(e);
        }finally{
            Long endTime = resultMap.get(Constant.END_DATE_KEY);
            OtherParamUtils.remove(resultMap,"System",Constant.RETURN_CODE,Constant.RETURN_MESSAGE,Constant.END_DATE_KEY);
            if(!StringUtils.isEmpty(version)){
                if(null == endTime) {endTime = System.currentTimeMillis();}
                returnMap.put("endTime",DateFormatUtils.format(endTime,"yyyy-MM-dd HH:mm:ss"));
            }
            returnMap.put(Constant.RETURN_CODE,status);
            //如果成功
            if(ReturnCodeEnum.SUCCESS.getCode().equals(stauts)){
                returnMap.put("data",resultMap);
            }else{
                returnMap.put(Constant.RETURN_MESSAGE,"服务编排异常"+info);
                logger.error(info);
            }
            returnStr = JSON.toJSONString(returnMap);
        }
        return returnStr;
    }
}
